package cn.edu.nju.ws.sview.wyge.servlets;

import java.io.IOException;
import java.net.URLDecoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

import cn.edu.nju.ws.sview.cache.GroupNameIDCache;
import cn.edu.nju.ws.sview.views.Group;
import cn.edu.nju.ws.sview.views.User;
import cn.edu.nju.ws.sview.views.View;
import cn.edu.nju.ws.sview.views.data.ViewEditing;
import cn.edu.nju.ws.sview.wyge.relations.adataset.relations.Relation;

/**
 * Servlet implementation class Entity
 */
public class RelationInfo extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public RelationInfo() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@SuppressWarnings("unchecked")
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String json = null;
		response.setCharacterEncoding("utf-8");
		if (request.getParameter("data") != null) {
			json = request.getParameter("data");
			String str = URLDecoder.decode(json, "UTF-8");
			JSONObject regtJSON = (JSONObject) JSONValue.parse(str);
			int id = Integer.parseInt((String) regtJSON.get("id"));
			System.out.println("relid:"+id+"|"+Relation.getInstance(id));
			Relation rel = Relation.getInstance(id);
			String name = rel.getName();
//			String user = rel.getUser();
			String group = rel.getGroup();
			String date = rel.getDate();
			int gid = Group.getGroupIdByName(group);
//			int ov_id = rel.getOv_id();
			JSONObject result = new JSONObject();
			result.put("name", name);
			result.put("group", group);//************
			result.put("date", date);
			result.put("gid", gid);//for ds_relations.js getRelationMeta
			
//			result.put("ov_id", ov_id);
			if(regtJSON.get("user") != null){
				String uname = (String)regtJSON.get("user");
				User user = new User(uname);
				String canedit = null;
//				if(user.getGroups().contains(Group.getGroupIdByName(group))){
				String ugname = (String)request.getSession().getAttribute("groupName");
//				if(Group.isMember(gid, user.getUserIdByName(uname))){
				if(gid == Group.getGroupIdByName(ugname)){
					canedit="true";
				}else{
					canedit="false";
				}
				result.put("canedit", canedit);
			}
			response.getWriter().print(result);
		}else if(request.getParameter("checkRelNameConflict") != null){
			json = request.getParameter("checkRelNameConflict");
			String str = URLDecoder.decode(json, "UTF-8");
			JSONObject regtJSON = (JSONObject) JSONValue.parse(str);
			String name = (String)regtJSON.get("name");
			int groupID = Integer.parseInt((String)regtJSON.get("groupID"));
			int rel_id = Integer.parseInt((String)regtJSON.get("rel_id"));
			String op = "ok";
			//若同一ov_id下存在于当前re_id相同且同名的relation，那么
			//提示是否覆盖，否则显示名称冲突			
//			System.out.println("in RelationInfo:"+str.toString());
			int existRelId = Relation.getRelationIdByNameGroupClassID(name, groupID);
			System.out.println("exist:"+existRelId);
			if(existRelId>0 && existRelId != rel_id){
				// 如果翻译后的名字已经存在，但是它也是原有datamember的重命名，那么
				// 名称不存在冲突（等到翻译总保存时覆盖该datamember）
				op = "conflict";
				if(ViewEditing.isSameOV(existRelId, rel_id)){
					op="ask";
				}
			}
			response.getWriter().print(op) ;
//			System.out.println("dd:"+existRelId+"|"+ViewEditing.isSameOV(existRelId, rel_id)+"|"+op);
//			if(op.contentEquals("conflict"))
//				response.getWriter().print("conflict") ;
//			else if(op.equals("ask"))
//				response.getWriter().print("ask") ;
//			else
//				response.getWriter().print("ok");
		}
	}
}
